// Important modules this config uses
const path = require('path');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const pkg = require(path.resolve(process.cwd(), 'package.json'));

module.exports = require('./webpack.base.babel')({
  // In production, we skip all hot-reloading stuff
  entry: {
    main: path.join(process.cwd(), 'app/app.js'),
    vendor: [
      'core-js',
      'lodash',
      'babel-polyfill', 
      'eventsource-polyfill', 
      'antd',
      'classnames',
      'object-path',
      'react',
      'react-dom',
      'react-redux',
      'react-router',
      'react-router-redux',
      'redux',
      'redux-actions',
      'redux-thunk',
      'redux-saga'
    ]
  },

  // Utilize long-term caching by adding content hashes (not compilation hashes) to compiled assets
  output: {
    filename: '[name].[chunkhash].js',
    chunkFilename: '[name].[chunkhash].chunk.js',
  },

  // add babelQuery
  // babelQuery: pkg.babel, 

  // We use ExtractTextPlugin so we get a separate CSS file instead
  // of the CSS being in the JS and injected as a style tag
  cssLoaders: 'style-loader!css-loader?modules&importLoaders=1&localIdentName=[name]__[local]___[hash:base64:5]!postcss-loader',

  // @todo:
  // ExtracTextPlugin 会导致 css import 报错
  // 可能的原因是 node 版本不兼容
  // ExtractTextPlugin.extract({
  //   fallbackLoader: 'style-loader',
  //   loader: 'css-loader?modules&-autoprefixer&importLoaders=1!postcss-loader',
  // }),

  plugins: [
    new webpack.optimize.CommonsChunkPlugin({
      name: 'vendor',
      filename: '[name].[chunkhash].js',
      chunks: ['main', 'vendor']
    }),

    // OccurrenceOrderPlugin is needed for long-term caching to work properly.
    // See http://mxs.is/googmv
    // new webpack.optimize.OccurrenceOrderPlugin(true),

    // @todo:
    // 该插件导致部分模块缺失
    // Merge all duplicate modules
    // new webpack.optimize.DedupePlugin(),

    // Minify and optimize the JavaScript
    // new webpack.optimize.UglifyJsPlugin({
    //   compress: {
    //     warnings: false, // ...but do not show warnings in the console (there is a lot of them)
    //   },
    // }),

    // Minify and optimize the index.html
    new HtmlWebpackPlugin({
      template: 'app/index.html',
      // minify: {
      //   removeComments: true,
      //   collapseWhitespace: true,
      //   removeRedundantAttributes: true,
      //   useShortDoctype: true,
      //   removeEmptyAttributes: true,
      //   removeStyleLinkTypeAttributes: true,
      //   keepClosingSlash: true,
      //   minifyJS: true,
      //   minifyCSS: true,
      //   minifyURLs: true,
      // },
      inject: true
    })
  ]
});
